import { Component, OnInit } from "@angular/core";
import { CommService } from "./comm-service";
import { Router } from "@angular/router";
import { MyhttpService } from "./myhttp.service";
import { LocalStorageService } from "./local-storage-service";

declare var swal: any;
declare var $: any;

@Component({
  template: `
    <div class="uppwdRoom">
      <div class="row">
        <div class="col-lg-12">
          <div class="portlet">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="backToLogin()">
              <span aria-hidden="true">&times;</span>
            </button>
            <h3 class="page-title">用户中心</h3>
            <div class="page-bar">
              <ul class="page-breadcrumb">
                <!-- <li>
                  <i class="fa fa-home"></i>
                  <a href="javascript:void(0)">{{loginname}}</a>
                  <i class="fa fa-angle-right"></i>
                </li> -->
                <li>
                  <a href="javascript:void(0)">{{label}}</a>
                </li>
              </ul>
              <!-- <div class="pull-right">
                <div class="btn2" style="background-color:#d5dade">
                  <i class="fa fa-file-image-o" style="color:#FFFFFF; font-size:22px; margin-top:10px"></i>
                </div>
              </div> -->
            </div>
          </div>
        </div>
      </div>
      <div class="row">

        <div class="first" style="display:block">
          <div class="dyb">输&nbsp;&nbsp;入&nbsp;&nbsp;账&nbsp;&nbsp;号</div>
          <hr/>
          <form>
            <div class="alert alert-danger display-block" *ngIf="noReactiveFormField.wdivs">
              <button class="close" (click)="closeWarn()"></button>
              <span>{{noReactiveFormField.wstv}}</span>
            </div>
            <label for="exampleInput">请输入你需要找回登录密码的手机号</label>
            <!-- <div class="row"> -->
              <!-- <div class="col-lg-8"> -->
                <div class="form-group">
                  
                  <input type="text" class="form-control" placeholder="手机号" [(ngModel)]="noReactiveFormField.mobile" name="mobile" (focus)="closeWarn()" />
                </div>
              <!-- </div> -->
              <!-- <div class="col-lg-4">
                <div *ngIf="noReactiveFormField.mobilewarn"
                     style="background-color: #FFEFEF;width: 100%;height: 33px;text-align: center;">
                  <span style="line-height: 33px;color: red">{{noReactiveFormField.mobilewarntext}}</span>
                </div>
              </div> -->
            <!-- </div> -->
            <button type="text" class="btn btn-default blue" style="width:390px;height:40px;" (click)="getuserinfo()">
              下一步
            </button>
          </form>
        </div>
        <div class="second" style="display:none">
          <div class="dyb">选&nbsp;&nbsp;择&nbsp;&nbsp;修&nbsp;&nbsp;复&nbsp;&nbsp;方&nbsp;&nbsp;式</div>
          <hr/>
          <form>
            <div class="alert alert-danger display-block" *ngIf="noReactiveFormField.wdivs">
              <button class="close" (click)="closeWarn()"></button>
              <span>{{noReactiveFormField.wstv}}</span>
            </div>
            <div class="tsnr">您正在对{{xgyhname}}进行重置密码的操作，请先进行安全验证:</div>
            <label for="exampleInputEmail1">点击获取验证码，短信将发送至安全手机{{noReactiveFormField.mobile}}</label>
            <!-- <div class="row"> -->
              <!-- <div class="col-lg-8"> -->
                <div class="form-group">                 
                  <div class="input-group" style="height:33px;">
                    <input type="text" style="height:33px;" class="form-control" placeholder="请输入验证码" name="moblieyzm"  [(ngModel)]="noReactiveFormField.moblieyzm" (focus)="closeWarn()"/>
                    <span class="input-group-addon" id="basic-addon1" style="cursor: pointer;" (click)="getmoblieYzm()">{{btnMsg}}</span>
                  </div>
                </div>
              <!-- </div> -->
              <!-- <div class="col-lg-4">
                <div *ngIf="noReactiveFormField.moblieyzmwarn"
                     style="background-color: #FFEFEF;width: 100%;height: 33px;text-align: center;">
                  <span style="line-height: 33px;color: red">{{noReactiveFormField.moblieyzmwarntext}}</span>
                </div>
              </div> -->
            <!-- </div> -->
            <button type="submit" class="btn btn-default blue" style="width:390px;height:40px;" (click)="secondNext()">下一步</button>
          </form>
        </div>
        <div class="third" style="display:none">
          <div class="dyb">设&nbsp;&nbsp;置&nbsp;&nbsp;新&nbsp;&nbsp;密&nbsp;&nbsp;码</div>
          <hr/>
          <form>
            <div class="alert alert-danger display-block" *ngIf="noReactiveFormField.wdivs">
              <button class="close" (click)="closeWarn()"></button>
              <span>{{noReactiveFormField.wstv}}</span>
            </div>
            <label for="exampleInputEmail1">请设置{{xgyhname}}的新密码，建议使用数字、字母、字符的组合密码，提高密码安全等级</label>
            <!-- <div class="row"> -->
              <!-- <div class="col-lg-8"> -->
                <div class="form-group">
                  
                  <input type="password" class="form-control" id="mima" name="newpwd" placeholder="8-16位密码，区分大小写"
                         (keyup)="newpwdChange()" (focus)="showProp()"  [(ngModel)]="noReactiveFormField.newpwdv" />
                  <div id="tips">
                    <span></span>
                    <span></span>
                    <span></span>
                    <label>{{noReactiveFormField.newpwdqdtx}}</label>
                  </div>
                </div>
              <!-- </div> -->
              <!-- <div class="col-lg-4">
                <div id="gzPpopover"></div>
                <div id="gzerror" *ngIf="noReactiveFormField.newpwdwarn">
                  <span>{{noReactiveFormField.newpwdwarntext}}</span>
                </div>
              </div> -->
            <!-- </div> -->

            <button type="submit" class="btn btn-default blue" style="width:390px;" (click)="sureMm()">确定</button>
          </form>

        </div>
      </div>
      <!--确认 / 返回-->
      <!-- <div class="actionbtn">
        <button class="btn btn-danger" type="button" >关 闭</button>&nbsp;&nbsp;
        <button class="btn btn-info" type="button" >确 认</button>
      </div> -->
    </div>
  `,
  styles: [
    ".uppwdRoom{width: 800px;margin: 50px auto;height: 800px;background-color: #fff;padding: 20px;border-radius: 10px;position: relative}",
    ".uppwdRoom>.actionbtn{position: absolute;bottom: 20px;right: 20px;}",
    "#xgpwdRoom{display: none}",
    "#wsxxRoom{display: none}",
    "#tips{font-size: 12px;width: 400px;height: 25px;margin: 5px 0 0 0px;}",
    "#tips>span{background: #D1D1D1;float: left;width: 60px;height: 15px;overflow: hidden;margin-right: 10px;line-height: 20px;}",
    "#tips label{color:#698bac }",
    "#tips .init{background-color:#D1D1D1 }",
    // 密码强度  弱,  只画一个span
    "#tips .s0{background-color:#E93106}",
    // 密码强度  中，画前两个span
    "#tips .s1{background-color: #FF8400}",
    // 密码强度 高，三个span都画
    "#tips .s2{background-color: #5BBA48}",
    // 验证规则列表 弹框
    "#gzPpopover{width: 1px;height: 33px;margin-top: 10px;visibility: hidden}",
    "#gzerror{background-color: #FFEFEF;width: 100%;height: 33px;text-align: center;}",
    "#gzerror>span{line-height: 33px;color: red}",
    ".redstart{color:red;display:inline-block;width:20px;vertical-align: middle; text-align:center;}",
    ".dyb{font-size:30px;margin-bottom:50px;text-align:center;}",
    "form{margin:100px 200px;}",
    ".tsnr{border: 1px solid #ccc;padding: 10px;background: #f7f7f7;margin-bottom:15px;}",
  ]
})
export class WjmmComponent implements OnInit {
  userinfo: any;
  label: string = "重置密码";
  xgyhname: string;
  o: string = '';
  btnMsg: string = "获取验证码";
  cutdowntimev: number = 120;
  cutdownLock: any;
  cansend: boolean = true;
  noReactiveFormField: any = {
    wdivs: false,
    wstv: '',
    newpwdv: "",
    newpwdwarn: false,
    newpwdwarntext: "",
    newpwdqdtx: "密码强度低",
    qrpwdv: "",
    rqpwdwarn: false,
    rqpwdwarntext: "",
    mobile: "",
    mobilewarn: false,
    mobilewarntext: "",
    moblieyzm: "",
    moblieyzmwarn: false,
    moblieyzmwarntext: "",
  };
  // 返回登录页
  backToLogin(): void {
    this.router.navigate(["login"]);
  }
  closeWarn(): void {
    this.noReactiveFormField.wdivs = false;
    this.noReactiveFormField.wstv = "";
  }
  /**
   * 第一步：根据手机号获取登陆用户名列表
   */
  getuserinfo(): void {
    const mobilev = this.commservice.removeSpace(true, this.noReactiveFormField.mobile);
    if (!mobilev) {
      this.noReactiveFormField.wdivs = true;
      this.noReactiveFormField.wstv = "手机号不能为空";
      return
    }
    this.noReactiveFormField.wdivs = false;
    this.noReactiveFormField.wstv = "";
    const regex = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
    const valid = regex.test(mobilev);
    if (!valid) {
      this.noReactiveFormField.wdivs = true;
      this.noReactiveFormField.wstv = "无效的手机号码";
      return
    }
    this.noReactiveFormField.wdivs = false;
    this.noReactiveFormField.wstv = "";
    // this.commservice.blockUI({
    //   boxed: true,
    //   message: "加载中...",
    //   zIndex: 10051,
    //   overlayColor: "#67809F"
    // });
    // const url = "manager/user/getUserNames";
    // this.itservice.kwPtPost(url, {
    //   mobile: this.mobile
    // }).subscribe((resp) => {
    //   this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
    //     if (arg1) {
    //       if (arg2) {
    //         this.xgyhname = resp.userNames;
    //         $(".second").show();
    //         $(".first").hide();
    //       }
    //     }
    //   })
    // })
    this.xgyhname = "测试中学管理员";
    $(".second").show();
    $(".first").hide();
  }


  /**
   * 第二步：根据手机号获取验证码
   */
  getmoblieYzm(): void {
    if (!this.cansend) {
      return
    }

    if (this.cutdownLock) {
      clearInterval(this.cutdownLock);
    }
    // 倒计时开始
    this.cutdowntimev = 120;
    this.cutdowntimev--;
    this.btnMsg = `${this.cutdowntimev}秒`;
    this.cansend = false;
    // 获取手机验证码 （和 倒计时2个线程，获取成功不影响倒计时）
    // const url = "manager/sms/sendMsg";
    // this.itservice.kwPtPost(url, {
    //   smsCodeType: "change",
    //   mobile: mobilev
    // }).subscribe(resp => {
    //   this.commservice.handResponse("put", url, resp, (arg1, resp, arg2) => {
    //     // 此接口没有返回信息
    //     if (arg1) {

    //     }
    //   });
    // });

    // 倒计时
    this.cutdownLock = setInterval(() => {
      this.cutdowntimev--;
      this.btnMsg = `${this.cutdowntimev}秒`;
      if (this.cutdowntimev == 0) {
        clearInterval(this.cutdownLock);
        this.btnMsg = "获取验证码";
        this.cansend = true;
      }
    }, 1000);
  }
  /**
   * 进行第3步 设置新密码
   */
  secondNext(): void {
    const mobimoblieyzmlev = this.commservice.removeSpace(true, this.noReactiveFormField.moblieyzm);
    if (!mobimoblieyzmlev) {
      this.noReactiveFormField.wdivs = true;
      this.noReactiveFormField.wstv = "验证码不能为空";
      return
    }
    this.noReactiveFormField.wdivs = false;
    this.noReactiveFormField.wstv = "";
    $(".third").show();
    $(".second").hide();
    $(".first").hide();

  }
  newpwdChange() {
    $("#gzPpopover").popover("hide");
    $("#gzPpopover").hide();
    const qdnum = this.pwdqdjb();
    // 还原为最初
    $("#tips span")[0].className = "init";
    $("#tips span")[1].className = "init";
    $("#tips span")[2].className = "init";
    if (qdnum == 0) {
      this.noReactiveFormField.newpwdqdtx = "密码强度低";
    } else if (qdnum == 1) {
      this.noReactiveFormField.newpwdqdtx = "密码强度弱";
      $("#tips span")[0].className = "s0";
    } else if (qdnum == 2) {
      this.noReactiveFormField.newpwdqdtx = "密码强度中";
      $("#tips span")[0].className = "s1";
      $("#tips span")[1].className = "s1";
    } else if (qdnum == 3) {
      this.noReactiveFormField.newpwdqdtx = "密码强度高";
      $("#tips span")[0].className = "s2";
      $("#tips span")[1].className = "s2";
      $("#tips span")[2].className = "s2";
    }
  }

  pwdqdjb(): number {
    const newpwd = this.noReactiveFormField.newpwdv ? this.noReactiveFormField.newpwdv : "";
    let retnum: number = 0;
    if (!newpwd) {
      this.noReactiveFormField.wdivs = true;
      this.noReactiveFormField.wstv = "新密码不能为空";
      retnum = 0;
    } else {
      if (newpwd.trim().length < 8 || newpwd.trim().length > 16) {
        this.noReactiveFormField.wdivs = true;
        this.noReactiveFormField.wstv = "新密码长度为8-16位";
        retnum = 0;
      } else {
        const regex = /^[0-9a-zA-Z]+$/;  // 数字或者字母
        const regex2 = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/;  //特殊字符
        const regex3 = /^[^\u4e00-\u9fa5]+$/; // 不能包含中文的字符串
        if (!regex3.test(newpwd)) {
          this.noReactiveFormField.wdivs = true;
          this.noReactiveFormField.wstv = "新密码不合法";
          retnum = 0;
        } else {
          // 符合规则， 判断 强度
          this.noReactiveFormField.wdivs = false;
          this.noReactiveFormField.wstv = "";
          retnum = 0;   // 初始化强度为  无
          if (newpwd.trim().length >= 8) {
            if (regex.test(newpwd)) {
              // 位数大于等于8
              // 包含 数字或者字母
              retnum = 1;   //强度为 弱
            }
          }
          if (/[a-zA-Z]/.test(newpwd) && /[0-9]/.test(newpwd)) {
            // 位数不限制
            // 同时包含 字母，数字
            retnum = 2;   //强度为 中
          }
          if (newpwd.trim().length >= 10) {
            if (/[a-zA-Z]/.test(newpwd) && /[0-9]/.test(newpwd) && regex2.test(newpwd)) {
              // 位数大于等于10
              // 同时包含字母，数字，特殊字符
              retnum = 3;   //强度为 高
            }
          }
        }
      }
    }
    return retnum;
  }

  // 初始化 prop内容
  initgzprop(): void {
    let contentstr = "<div><i class='fa fa-info-circle'></i>&nbsp;<span>8-16位字符</span></div>";
    contentstr += "<div><i class='fa fa-info-circle'></i>&nbsp;<span>字母、数字、字符组成</span></div>";
    //contentstr += "<div><i class='fa fa-info-circle'></i>&nbsp;<span>只能包含字母、数字和下划线</span></div>";
    $("#gzPpopover").popover({
      html: true,
      content: contentstr
    });
  }

  // input获取焦点的时候，显示 prop
  showProp(): void {
    if (this.noReactiveFormField.oldpwdv) {
      this.noReactiveFormField.oldpwdwarn = false;
    } else {
      this.noReactiveFormField.oldpwdwarn = true;
    }
    this.noReactiveFormField.newpwdwarn = false;
    $("#gzPpopover").show();
    $("#gzPpopover").popover("show");
    this.closeWarn();
  }

  //  点击其他地方的时候，prop隐藏， 提示信息显示
  closeProp(): void {
    const that = this;
    $(".uppwdRoom").click(function (e) {
      const targetdom = e.target;
      if ($(targetdom).attr("name") != 'newpwd') {
        $("#gzPpopover").popover("hide");
        $("#gzPpopover").hide();
        if (that.noReactiveFormField.newpwdwarntext) {
          // 如果密码规则不通过才显示
          that.noReactiveFormField.newpwdwarn = true;
        }
      }
    });
  }


  /**
   * 最后确认密码，并返回登录页
   */
  sureMm(): void {
    const newpwdv = this.commservice.removeSpace(true, this.noReactiveFormField.newpwdv);
    if (!newpwdv) {
      this.noReactiveFormField.wdivs = true;
      this.noReactiveFormField.wstv = "新密码不能为空";
      return;
    }
    this.noReactiveFormField.wdivs = false;
    this.noReactiveFormField.wstv = '';
    this.router.navigate(["/login"]);
  }
  constructor(private commservice: CommService,
    private itservice: MyhttpService,
    private router: Router,
    private localStorageService: LocalStorageService) {

  }

  ngOnInit() {
    this.initgzprop();
    this.closeProp();
  }
}
